import { Injectable } from "@nestjs/common";
import { CreateRoleDto } from "./dto/create-role.dto";
import { UpdateRoleDto } from "./dto/update-role.dto";
import { ResultData } from "@/common/utils/result";
import { instanceToPlain } from "class-transformer";
import { InjectRepository } from "@nestjs/typeorm";
import { Like, Repository } from "typeorm";
import { Role } from "@/modules/role/entities/role.entity";
import { User } from "@/modules/user/entities/user.entity";
import { UserRole } from "@/modules/user-role/entities/user-role.entity";


@Injectable()
export class RoleService {
	constructor(
		@InjectRepository(Role)
		private readonly roleRepository: Repository<Role>,
		@InjectRepository(User)
		private readonly userRepository: Repository<User>,
		@InjectRepository(UserRole)
		private readonly userRoleRepository: Repository<UserRole>
	) {
	}
	
	async create(createRoleDto: CreateRoleDto) {
		return ResultData.ok(instanceToPlain(await this.roleRepository.save(createRoleDto)));
	}
	
	async findAll(dto) {
		const { page, size, name } = dto;
		const take = size || 15;
		const skip = ((page || 1) - 1) * take;
		const where = {
			...(name ? { name: Like(`%${name}%`) } : null)
		};
		return ResultData.rowOk(instanceToPlain(await this.roleRepository.findAndCount({
			order: {
				id: "DESC"
			},
			where,
			skip,
			take
		})));
	}
	
	findOne(id: number) {
		return `This action returns a #${id} role`;
	}
	
	update(id: number, updateRoleDto: UpdateRoleDto) {
		return `This action updates a #${id} role`;
	}
	
	remove(id: number) {
		return `This action removes a #${id} role`;
	}
	
	
}
